package activemqDemo;

import org.slf4j.Logger;  
import org.slf4j.LoggerFactory;  
import org.springframework.jms.core.JmsTemplate;  
import org.springframework.jms.core.MessageCreator;  
  
import javax.jms.JMSException;  
import javax.jms.Message;  
import javax.jms.Session;  
import javax.jms.TextMessage;  
  
/** 
 * ActiveMQ 消息生产类 
 */  
  
public class MessageSender {  
    private Logger logger = LoggerFactory.getLogger(this.getClass());  
  
    private String defaultQueueName = "default_queue";   //默认消息队列名称  
    private String integralQueueName = "integral_queue"; //积分消息队列名称  
  
    private JmsTemplate jmsTemplate;  
  
    public void setJmsTemplate(JmsTemplate jmsTemplate) {  
        this.jmsTemplate = jmsTemplate;  
    }  
  
    /** 
     * 发送到默认消息队列 
     * @param message 
     */  
    public void defaultMessage(String message) {  
        System.out.println("发送了一条消息到默认队列.......");  
            send(message, 0);  
    }  
  
    /** 
     * 发送到积分消息队列 
     * @param message 
     */  
    public void integraMessage(String message) {  
        System.out.println("发送了一条消息到积分队列.......");  
        send(message, 1);  
    }  
  
    /** 
     * 发送消息 
     * @param message 
     * @param type 0:默认队列 1：积分队列 
     */  
    public void send(final String message, int type) {  
        try {  
            String destination = type==0? defaultQueueName:integralQueueName;  
            jmsTemplate.send(destination, new MessageCreator() {  
                @Override  
                public Message createMessage(Session session) throws JMSException {  
                    TextMessage textMessage = session.createTextMessage(message);  
                    return textMessage;  
                }  
            });  
        } catch (Exception e) {  
            logger.error("发送消息失败", e);  
        }  
    }  
} 
